package com.yn.front.driver;


import com.yn.common.enums.UserIdentityEnum;
import com.yn.common.exception.TransitException;
import com.yn.common.model.user.User;
import com.yn.common.page.BasePage;
import com.yn.common.request.driver.DeparturePlanRequest;
import com.yn.common.request.driver.ReturnPlanModifyRequest;
import com.yn.common.request.sc.PageParamRequest;
import com.yn.common.request.zy.ReturnPlanQuery;
import com.yn.common.response.driver.DepartureDetailResponse;
import com.yn.common.response.driver.DeparturePlanResponse;
import com.yn.common.utils.genutils.CommonResult;
import com.yn.service.service.ICarDeparturePlanService;
import com.yn.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 司机返程计划表 前端控制器
 * </p>
 *
 * @author xuquanjun
 * @since 2025-07-02
 */
@RestController
@RequestMapping("api/driver/order")
@Slf4j
@Api(tags = "司机返程计划——司机端")
public class CarDeparturePlanController {
    @Resource
    private ICarDeparturePlanService carDeparturePlanService;

    @Resource
    private UserService userService;


    /**
     * 司机发单——返程订单
     * @param request 司机返程计划请求对象
     * @return
     */
    @ApiOperation(value = "司机发布返程计划")
    @PostMapping(value = "/publish")
    public CommonResult<Integer> DriverPublishOrder(@RequestBody @Validated DeparturePlanRequest request) {
        log.info("司机发布返程计划：request:{}", request);
       Integer id= carDeparturePlanService.DriverPublishOrder(request);
        return CommonResult.success(id);
    }

    /**
     * 司机端删除返程订单
     * */
    @ApiOperation(value = "司机删除返程计划")
    @PostMapping(value = "/delete")
    public CommonResult<String> delete(@RequestParam(required = true) @NotNull Integer id) {
        carDeparturePlanService.deleteById(id);
        return CommonResult.success();
    }


    /**
     * 修改订单信息
     * @param request 司机返程计划修改请求对象
     * @return
     */
    @ApiOperation(value = "修改订单信息")
    @PostMapping(value = "/update")
    public CommonResult<String> update(@RequestBody @Validated ReturnPlanModifyRequest request) {
        //获取用户信息
        User user = userService.getInfoException();
        //只有车队入驻人员或者后台管理人员可以修改
        if(!Objects.equals(user.getIdentity(), UserIdentityEnum.FLEET_ENTRY_PERSONNEL.getCode()) ){
            throw new TransitException("您没有权限修改该订单");
        }
        carDeparturePlanService.updateReturnPlan(request,user.getUid());
        return CommonResult.success();
    }


    /**
     * 司机查看自己发单列表
     * */

    @ApiOperation(value = "返程计划列表")
    @GetMapping(value = "/departure/list")
    public CommonResult<BasePage<DeparturePlanResponse>> MyOrderList(ReturnPlanQuery query, PageParamRequest pageRequest) {
        User user = userService.getInfoException();
        BasePage<DeparturePlanResponse> response= carDeparturePlanService.orderList(query,pageRequest,user);
        return CommonResult.success(response);
    }

    /**
     * 查看返车计划详情
     * @param id
     * @return
     */
    @ApiOperation(value = "查询订单详情")
    @GetMapping(value = "/departure/detail")
    public CommonResult<DepartureDetailResponse> OrderDetail(@RequestParam @NotNull Integer id) {
        User user = userService.getInfoException();
        DepartureDetailResponse response= carDeparturePlanService.OrderDetail(id,user);
        return CommonResult.success(response);
    }

}
